package com.tj.test.sys.controller;

import com.tj.test.sys.po.Power;
import com.tj.test.sys.service.impl.PowerService;
import com.tj.test.sys.tool.ResponseMessage;
import com.tj.test.sys.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/*对外提供数据服务接口*/
@Api(tags="power")
@Controller
@CrossOrigin
@RequestMapping("/power")
public class PowerController {
    @Autowired
    private PowerService service;

    @RequestMapping(value = "/selectAll",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("查询所有权限信息")
    public ResponseMessage selectAllPower(){
        return Result.success(service.selectAllPower());}

    @RequestMapping(value = "/insert",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("插入权限信息")
    public ResponseMessage insertPower(Power power) {
        int temp=0;
        //传入数据的判断
        //(1)实体对象不能为空
        if (power != null) {
            //（2）权限id不能为空
            if (power.getPowerid() != 0 ) {
                //(3)添加权限信息
                temp=service.insertPower(power);
                if (temp>0) {return Result.success("插入成功",temp);}
            }
        }
        return Result.error("插入数据失败",temp);
    }
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("删除权限信息")
    public ResponseMessage deletePower(Power power){
        int temp=0;
        try {
            if (power!=null){
                temp=service.deletePower(power);
                if (temp>0) {
                    return Result.success("删除成功",temp);
                }else {
                    return Result.error("删除失败，未找到该条信息");}
            }
        }
        catch (Exception e){
            temp=-1;
            return Result.error("删除失败，数据库异常",temp);
        }
        return Result.error("删除失败，数据为空",temp);}

    @RequestMapping(value = "/update",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("更新权限信息")
    public ResponseMessage updatePower(Power power){
        int temp=0;
        try {
            if (power!=null){
                temp=service.updatePower(power);
                if (temp>0) {
                    return Result.success("更新成功",temp);
                }else {
                    return Result.error("更新失败，未找到该条信息");}
            }
        }
        catch (Exception e){
            temp=-1;
            return Result.error("更新失败，数据库异常",temp);
        }
        return Result.error("更新失败，数据为空",temp);}
}

